﻿package com.toncen.samepms.system.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.system.domain.RoleBlockRight;

/**
 * 描述：系统角色模块权限分配表表SYSTEM_ROLE_BLOCK_RIGHT的dao文件
 * 
 * @author tch
 */
public class RoleBlockRightDao  extends AbstractDao<RoleBlockRight> {

	public RoleBlockRightDao() {
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("RoleBlockRight");
		setStatementDivision(".");
	}
	/**
	 * 取用户权限
	 * 
	 * @param map
	 * @return
	 * @throws SQLException 
	 */
	public List getByRoleIdAndBlockNum(
			Map<String, String> map) throws SQLException {
		List list=getSqlMap().queryForList(getStatement("getByRoleIdAndBlockNum"), map);
				return list;
	}
	
	/**
	 * 更新权限
	 * 
	 * @param groupIds
	 * @param systemId
	 * @param modleId
	 * @param isName
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public void batchSaveBlockRight(HttpServletRequest request,Long roleId)
			throws Exception {
		
	
		java.util.Enumeration eu = request.getParameterNames();
		String tempParName = "";
		String tempParValue = "";
		String tagId = "isChecked";
		String serviceId = "";

		// 更新或者新增对应的项：先删除，后新增
		getSqlMap().delete(getStatement("RoleBlockRight_delByRoleId"),
				roleId);
		while (eu.hasMoreElements()) {
			tempParName = "";
			tempParValue = "";
			tempParName = (String) eu.nextElement();
			RoleBlockRight roleBlockRight = new RoleBlockRight();
			if (tempParName != null && tempParName.startsWith(tagId)) {
				// 以某一前缀开头，就是对应的项 ; 如果isName是true，就是获取名称中的值，否则就是名称对应的值
				tempParValue = request.getParameter(tempParName);
				serviceId = tempParName.replaceAll(tagId, "");
				if (serviceId.startsWith("Add")) {
					roleBlockRight.setRightCode("addRight");
					roleBlockRight.setBlockNum(serviceId.substring(3,
							serviceId.length()));
				} else if (serviceId.startsWith("Del")) {
					roleBlockRight.setRightCode("delRight");
					roleBlockRight.setBlockNum(serviceId.substring(3,
							serviceId.length()));
				} else if (serviceId.startsWith("Modify")) {
					roleBlockRight.setRightCode("modifyRight");
					roleBlockRight.setBlockNum(serviceId.substring(6,
							serviceId.length()));
				} else if (serviceId.startsWith("View")) {
					roleBlockRight.setRightCode("viewRight");
					roleBlockRight.setBlockNum(serviceId.substring(4,
							serviceId.length()));
				} else if (serviceId.startsWith("EditSelect")) {
					roleBlockRight.setRightCode("editSelectRight");
					roleBlockRight.setBlockNum(serviceId.substring(10,
							serviceId.length()));
				} else if (serviceId.startsWith("ImportExcel")) {
					roleBlockRight.setRightCode("importExcelRight");
					roleBlockRight.setBlockNum(serviceId.substring(11,
							serviceId.length()));
				} else if (serviceId.startsWith("ExportExcel")) {
					roleBlockRight.setRightCode("exportExcelRight");
					roleBlockRight.setBlockNum(serviceId.substring(11,
							serviceId.length()));
				}else if (serviceId.startsWith("Copy")) {
					roleBlockRight.setRightCode("copyRight");
					roleBlockRight.setBlockNum(serviceId.substring(4,
							serviceId.length()));
				} else if (serviceId.startsWith("Enable")) {
					roleBlockRight.setRightCode("enableRight");
					roleBlockRight.setBlockNum(serviceId.substring(6,
							serviceId.length()));
				} else if (serviceId.startsWith("UnEnable")) {
					roleBlockRight.setRightCode("unEnableRight");
					roleBlockRight.setBlockNum(serviceId.substring(8,
							serviceId.length()));
				} else if (serviceId.startsWith("CheckBox")) {
					roleBlockRight.setRightCode("checkBoxRight");
					roleBlockRight.setBlockNum(serviceId.substring(8,
							serviceId.length()));
				}
				roleBlockRight.setRightIsok(tempParValue);
				roleBlockRight.setRoleId(roleId);
				roleBlockRight.setBlockName("");
				save(roleBlockRight);
			}
		}
	}
}
